<?php

namespace App\Models;

use App\Handlers\KeysHandler;
use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Cache;

class Salesman extends Model
{

    protected $fillable = [
        'type',
        'contact',
        'forbid',
        'url'
    ];

    use SoftDeletes;
    use HasDateTimeFormatter;


    //是否禁用
    const NORMAL = 0;
    const FORBID = 1;


    const TYPE_MAP = [
        'Telegram' => 'Telegram',
        'WhatsApp' => 'WhatsApp',
        'Other' => 'Other',
    ];

    const ICON_MAP = [
        'Telegram' => 'swanShop/uploads/images/6abd6261657b8b32d267c41ae2e14381.jpg',
        'WhatsApp' => 'swanShop/uploads/images/ce49960c71ede8de8a7b8c6b666084f3.jpg',
        'Other' => 'swanShop/uploads/images/ce49960c71ede8de8a7b8c6b666084f3.jpg',
    ];

    const FORBID_MAP = [
        self::NORMAL => '启用',
        self::FORBID => '禁用'
    ];


    public function user(): HasMany
    {
        return $this->hasMany(User::class, 'salesmen_id');
    }

    /**
     * 获取业务员的用户总数
     * @param int $salesmen_id
     * @return int
     */
    public function getTotalUser(int $salesmen_id): int
    {
        return User::query()->where('salesmen_id', $salesmen_id)->count('id');
    }

    public function users(): HasMany
    {
        return $this->hasMany(User::class, 'salesmen_id', 'id');
    }


    public function userSalesman($user)
    {
        $user_id = $user ? $user->id : 0;
        $key = sprintf(KeysHandler::USER_SALESMEN, $user_id);
        $salesmen = Cache::get($key);
        if (!$salesmen) {
            if (!empty($user)) {
                $salesmen = $user->salesmen;
                if ($salesmen && $salesmen->forbid == self::NORMAL) {
                    if ($salesmen->type == 'WhatsApp') {
                        $salesmen->url = 'https://api.whatsapp.com/send?phone=' . $salesmen->contact;
                        $salesmen->contact = '+' . $salesmen->contact;
                    } else {
                        $salesmen->url = 'https://t.me/' . $salesmen->contact;
                        $salesmen->contact = '@' . $salesmen->contact;
                    }
                    Cache::put($key, $salesmen, KeysHandler::USER_SALESMEN_TTL);
                    return $salesmen;
                }
            }

            //没有客服或者客服未查找到就随机分配一个客服
            //分配业务员
            $salesmen = $this->distributionSalesman();
            if (!$salesmen) {
                return [];
            }
            if (!empty($user)) {
                $user->salesmen_id = $salesmen->id;
                $user->save();
            }
            Cache::put($key, $salesmen, KeysHandler::USER_SALESMEN_TTL);
            return $salesmen;
        }
        return $salesmen;


    }

    public function DataProcess($param)
    {
        if (!$param->isEmpty()) {
            foreach ($param as $value) {
                if ($value->type == 'WhatsApp') {
                    $value->url = 'https://api.whatsapp.com/send?phone=' . $value->contact;
                    $value->contact = '+' . $value->contact;
                } else {
                    $value->url = 'https://t.me/' . $value->contact;
                    $value->contact = '@' . $value->contact;
                }
            }
        }
        return $param;
    }

    //获取分配业务员信息
    public function distributionSalesman()
    {
        $full_salesmen = self::query()->withCount('users')->where('forbid', '=', self::NORMAL)->get();
        $full_salesmen = $this->DataProcess($full_salesmen);
        if (!count($full_salesmen)) {
            return [];
        }
        //已客户数量升序分配
        return $full_salesmen->sortBy('users_count')->first();
    }

}
